package com.shihb.structuralpatterns.adapterpatterns;/**
 *Description:适配器模式
 *Version:1.0.0
 *@author shihb
 *@date 2020/4/21 11:19
 */
public class AdapterPattern {

  public static void main(String[] args) {
    Adaptee adaptee = new Adaptee();
    Computer computer = new Computer();
    Adapter adapter = new Adapter();
    Adapter2 adapter2 =new Adapter2(adaptee);
    computer.net(adapter2);

  }
}
class Adaptee{
  // 网线有个请求可以上网
  public void request(){
    System.out.println("连接网线上网");
  }
}
class Computer {
  // 电脑要上网但不能插网线,那就插适配器
  public void net( Net2Usb Adapter){
    //通过适配器上网
    Adapter.handleRequest();
  }

}
/**
 * 定义一个规范
 */
interface Net2Usb {
  void handleRequest();
}

/**
 * 转换器实现这个规范，继承适配器，java单一继承局限性强
 */
class Adapter extends Adaptee implements Net2Usb{
  @Override
  public void handleRequest() {
    super.request();
  }
}
/**
 * 转换器实现这个规范，对象适配器，推荐
 */
class Adapter2 implements Net2Usb{
  private Adaptee adaptee;
  public Adapter2(Adaptee adaptee){
    this.adaptee=adaptee;
  }
  @Override
  public void handleRequest() {
    this.adaptee.request();
  }
}
